%%% Western Europe
%%%--------------------------

clear all; clc;
load dataEu3.txt; load investEu.txt;  
k=4; opt=2; nsim=100; mmax=5;
% drop Iceland for GTD dom, and Finland for Iterate
gdp={dataEu3(:,1:19) [dataEu3(:,1:8) dataEu3(:,10:19)] [dataEu3(:,1:3) dataEu3(:,5:19)]};
pop={dataEu3(:,20:38) [dataEu3(:,20:27) dataEu3(:,29:38)] [dataEu3(:,20:22) dataEu3(:,24:38)]}; 
ter={dataEu3(:,39:57) [dataEu3(:,58:65) dataEu3(:,67:76)] [dataEu3(:,77:79) dataEu3(:,81:95)]};
invst={investEu(:,1:19) [investEu(:,1:8) investEu(:,10:19)] [investEu(:,1:3) investEu(:,5:19)]};

resultsEu={};

for i = 1:3   % 1 = total ter; 2 == domestic ter; 3 == transnational ter;
        for j = 1:2 % 1 = ter/1000; 2 = ter/pop;
                for m=1:2 % 1 = level ter; 2 = 1st differenced ter;
                    [yy, ylag, tlag, ws, wn]=prepdat4(gdp{i}, ter{i}, invst{i}, pop{i}, j, m);
                    [T N]=size(yy);
                    xx = [tlag ws wn];
                    [bfinal1 pval mpy1 mpx mpu1 converg1] = fapr1(yy, ylag, xx, opt, nsim, mmax, k, N);
             
                    resultsEu.beta{i,j,m} = bfinal1;
                    resultsEu.pval{i,j,m} = pval;
                    resultsEu.stat{i,j,m} = [mpy1 mpx mpu1 converg1];
                    keep resultsEu dataEu3 investEu k opt nsim mmax ter gdp invst pop i j m
                end
        end
end



%%%%%-------------------------------------------------
%%%%%       RESULTS
%%%%%-------------------------------------------------


%%%% Results cell arrays are as follow:

%{1,1,1} = Total terrorism/1000 level
%{1,1,2} = Total terrorism/1000 first-differenced
%{1,2,1} = Total terrorism/pop level
%{1,2,2} = Total terrorism/pop first-differenced

%{2,1,1} = Domestic terrorism/1000 level
%{2,1,2} = Domestic terrorism/1000 first-differenced
%{2,2,1} = Domestic terrorism/pop level
%{2,2,2} = Domestic terrorism/pop first-differenced

%{3,1,1} = Transnat terrorism/1000 level
%{3,1,2} = Transnat terrorism/1000 first-differenced
%{3,2,1} = Transnat terrorism/pop level
%{3,2,2} = Transnat terrorism/pop first-differenced

%%%% Reading colums and rows of results: 
%%%% Columns: 1-2=total & pval; 3-4=Domestic & pval; 5-6=Trans & pval
%%%% Rows: 1-4 = lagy, ter/1000, invest, pop; 5-8 = lagy, ter/POP, invest, pop;


%%% Terrorism Level: full results
TableLevel=[resultsEu.beta{1,1,1} resultsEu.pval{1,1,1} resultsEu.beta{2,1,1} resultsEu.pval{2,1,1} ...
    resultsEu.beta{3,1,1} resultsEu.pval{3,1,1}; resultsEu.beta{1,2,1} resultsEu.pval{1,2,1} ...
    resultsEu.beta{2,2,1} resultsEu.pval{2,2,1} resultsEu.beta{3,2,1} resultsEu.pval{3,2,1}]

%%% Terrorism first-differenced: full results
TableFD=[resultsEu.beta{1,1,2} resultsEu.pval{1,1,2} resultsEu.beta{2,1,2} resultsEu.pval{2,1,2} ...
    resultsEu.beta{3,1,2} resultsEu.pval{3,1,2}; resultsEu.beta{1,2,2} resultsEu.pval{1,2,2} ...
    resultsEu.beta{2,2,2} resultsEu.pval{2,2,2} resultsEu.beta{3,2,2} resultsEu.pval{3,2,2}]

%%% Stats: number of factors y, number of factors x, number of factors u,
%%% convergence
TableStats=[resultsEu.stat{1,1,1}; resultsEu.stat{2,1,1}; resultsEu.stat{3,1,1}; ...
    resultsEu.stat{1,2,1}; resultsEu.stat{2,2,1}; resultsEu.stat{3,2,1}; ... 
    resultsEu.stat{1,1,2}; resultsEu.stat{2,1,2}; resultsEu.stat{3,1,2}; ...
    resultsEu.stat{1,2,2}; resultsEu.stat{2,2,2}; resultsEu.stat{3,2,2}]

%%%% Table 4: only results shown in Table 4
Table4=[resultsEu.beta{1,1,1}(2) resultsEu.beta{2,1,1}(2) resultsEu.beta{3,1,1}(2); ...
    resultsEu.beta{1,2,1}(2) resultsEu.beta{2,2,1}(2) resultsEu.beta{3,2,1}(2); ... 
    resultsEu.beta{1,1,2}(2) resultsEu.beta{2,1,2}(2) resultsEu.beta{3,1,2}(2); ...
    resultsEu.beta{1,2,2}(2) resultsEu.beta{2,2,2}(2) resultsEu.beta{3,2,2}(2)]
